﻿using DeHeng_Mes.Dao;
using DeHeng_Mes.Dao.PlcDao;
using DeHeng_Mes.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeHeng_Mes.Thread
{
    public class PowerGather
    {
        PlcDao plcDao = new PlcDao();

        ProductionPowerDao productionPowerDao = new ProductionPowerDao();

        // 缓存当前小时的能耗
        string power1 = string.Empty;
        string power2 = string.Empty;

        // 缓存当前小时
        int realHour = DateTime.Now.Hour;

        // 缓存当前天
        DateTime realDate = DateTime.Today;

        public async Task GetHourPower()
        {
            Tools.ProduceLogText("启动小时能耗采集");

            try
            {
                while (true)
                {
                    // 获取当前日期
                    DateTime today = DateTime.Today;

                    // 获取当前小时
                    int hour = DateTime.Now.Hour;

                    // 处理跨天和小时变化
                    if (realHour != hour || realDate != today)
                    {
                        power1 = power2 = string.Empty;
                        realHour = hour;
                        realDate = today;
                    }

                    foreach (var item in PlcConnectionManager.plcContainer)
                    {
                        // 未连接跳出本层
                        if (!item.isConnected)
                            continue;

                        if (item.name == "DOOR-03" || item.name == "DOOR-04")
                            continue;

                        // 获取Plc名称
                        string plcName = item.name;

                        // 获取Plc数据
                        Dictionary<object, Type> dictionary = MappingTable.PlcMapping(item.plc, item.name);

                        // 执行当日能耗采集
                        string power = plcDao.GetStringFieldValue(dictionary, "Power", "RealTimePower");

                        // 缓存起始能耗
                        if (item.name == "DOOR-01" && string.IsNullOrEmpty(power1))
                        {
                            power1 = power;
                        }
                        else if (item.name == "DOOR-02" && string.IsNullOrEmpty(power2))
                        {
                            power2 = power;
                        }

                        if (string.IsNullOrEmpty(power1) || string.IsNullOrEmpty(power2))
                            continue;

                        // 执行小时能耗计算
                        double value = 0;

                        switch (item.name)
                        {
                            case "DOOR-01":
                                value = double.Parse(power) - double.Parse(power1);
                                break;
                            case "DOOR-02":
                                value = double.Parse(power) - double.Parse(power2);
                                break;
                        }

                        value = Math.Round(value, 2);

                        // 存储或更新数据
                        if (productionPowerDao.HourPowerIsSave(value, today, hour, plcName) && value != 0)
                            productionPowerDao.HourPowerUpDate(value, today, hour, plcName);
                    }

                    await Task.Delay(1000);
                }
            }
            catch (Exception ex)
            {
                Tools.ProduceLogText("小时能耗采集失败，原因是" + ex.Message); ;
            }
        }
    }
}
